<template>
  <div class="login">
    <el-input type="text" v-model="loginName" placeholder="登录名"></el-input>
    <el-input type="password" v-model="password" placeholder="密码"></el-input>

    <el-button round type="primary" @click="login">登录</el-button>
    <el-button round type="success" @click="registerfactory"
      >注册工厂</el-button
    >
    <el-button round type="success" @click="registeruser">注册用户</el-button>

    <!-- 注册工厂弹窗 -->
    <el-dialog title="注册工厂" :visible.sync="addfactoryDialog">
      <el-form :model="factoryaddForm" label-width="80px">
        <el-form-item label="工厂名称">
          <el-input
            v-model="factoryaddForm.factoryName"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="addFactory()">注册</el-button>
        <el-button @click="addfactoryDialog = false">取消</el-button>
      </el-form>
    </el-dialog>

    <!-- 注册用户弹窗 -->
    <el-dialog title="注册用户" :visible.sync="addUserDialog">
      <el-form :model="useraddForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input
            v-model="useraddForm.userName"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="登录名">
          <el-input
            v-model="useraddForm.loginName"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="useraddForm.password"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户权限">
          <el-input
            v-model="useraddForm.userLevel"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="工厂">
          <el-select v-model="useraddForm.factoryId" style="width:220px" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.factoryId"
              :label="item.factoryName"
              :value="item.factoryId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="addUser()">注册</el-button>
        <el-button @click="addUserDialog = false">取消</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
/* import func from '../../vue-temp/vue-editor-bridge'; */

export default {
  name: "home",
  data() {
    return {
      factoryId: this.$route.query.factoryId,
      loginName: "",
      password: "",
      factoryaddForm: {},
      useraddForm: {},
      addfactoryDialog: false,
      addUserDialog: false,
      options:[]
    };
  },
  mounted(){

  },
  methods: {
    login() {
      if (this.loginName == "") {
        alert("登录名称不能为空!");
        return;
      }
      if (this.password == "") {
        alert("密码不能为空!");
        return;
      }
      this.$axios
        .post(
          "UserController/getUserByIdByPass",
          this.$qs.stringify({
            loginName: this.loginName,
            password: this.password,
          })
        )
        .then((value) => {
          let user = value.data;
          if (user == null) {
            alert("用户名和密码不正确！");
          } else {
            console.log(user);
            this.$setSessionStorage("user", user);
            this.$router.push({
              path: "/home",
              query: { factoryId: this.factoryId },
            });
          }
        })
        .catch((value) => {
          console.error(value);
        });
    },
    registerfactory() {
      this.addfactoryDialog = true;
    },
    registeruser() {
      this.addUserDialog = true;
      this.getFactoryId();
    },

    getFactoryId() {
      this.$axios
        .post("FactoryController/getAllFactory")
        .then((value) => {
          this.options = value.data;
        })
        .catch((err) => {
          console.error(err);
        });
    },

    addUser() {
      if (this.useraddForm.userName == "") {
        alert("用户名不能为空");
        return;
      }
      if (this.useraddForm.loginName == "") {
        alert("登录名称不能为空");
        return;
      }
      if (this.useraddForm.password == "") {
        alert("密码不能为空");
        return;
      }
      if (this.useraddForm.userLevel == "") {
        alert("用户权限不能为空");
        return;
      }
      if (this.useraddForm.factoryId == "") {
        alert("工厂编号不能为空");
        return;
      }
      this.$axios
        .post("UserController/saveUser", this.$qs.stringify(this.useraddForm))
        .then((value) => {
          if (value.data > 0) {
            alert("注册成功！");
            this.addUserDialog = false;
          } else {
            alert("新增失败");
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    addFactory() {
      if (this.factoryaddForm.factoryName == "") {
        alert("工厂名不能为空");
        return;
      }
      this.$axios
        .post(
          "FactoryController/saveFactory",
          this.$qs.stringify(this.factoryaddForm)
        )
        .then((value) => {
          if (value.data > 0) {
            alert("注册成功！");
            this.addfactoryDialog = false;
          } else {
            alert("新增失败");
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
};
</script>

<style scoped>
.login {
  margin: 0 auto;
  margin-top: 100px;
  width: 400px;
  height: 200px;
}
</style>

